/*
 * @Description: 格式转换
 * @Author: Zzt
 * @Date: 2022-11-11 14:52:39
 * @LastEditTime: 2023-01-18 10:08:50
 */
import VisualizationMain from '@/components/DataVisualization/VisualizationMain';
import Nav from '@/components/Nav';
import { Tabs } from 'antd';
import { CheckboxValueType } from 'antd/es/checkbox/Group';
import classNames from 'classnames/bind';
import { useEffect, useState } from 'react';
import styles from '../styles/formatConversion.module.scss';
import { useFormatConversionContext } from '../contexts/FormatConversionContext';
const { TabPane } = Tabs;
const cx = classNames.bind(styles);

const according = [
  { fondName: '按年度统计', id: 0 },
  { fondName: '按授权管理', id: 1 },
  { fondName: '按操作者', id: 2 }
];
const segmentedList = [
  { label: '转换总次数', value: 'converNo' },
  { label: '文件转换数量', value: 'fileConverTotal' }
];
const table = [
  { title: '年度', dataIndex: 'yearName', key: 'yearName' },
  { title: '转换次数统计', dataIndex: 'converNo', key: 'converNo' }
];
const FormatConversionWarp = () => {
  const { setChartData, setTableData, dataList, chartList } = useFormatConversionContext();
  const [limit, setLimit] = useState<any>({});
  const [segmented, setSegmented] = useState('converNo');
  const [fondValue, setFondValue] = useState({});

  // 选中的按照类型
  const [type, setType] = useState(0);
  const [options, setOptions] = useState([]);
  const [visibleData, setVisibleData] = useState([]);
  const [condition, setCondition] = useState(<></>);
  const [tableList, setTableList] = useState(table);
  const [xData, setXData] = useState([]);
  const [yData, setYData] = useState([]);
  const [listQuery, setListQuery] = useState({
    pageNo: 1,
    pageSize: 10,
    param: {}
  });
  /**
   * @description: 切换全宗
   * @param {number} val 选中的全宗
   */
  const handleAccordingChange = (val: any) => {
    console.log(val);
    setType(parseInt(val, 10));
  };

  const onChange = (checkedValues: CheckboxValueType[]) => {
    console.log('checked = ', checkedValues);
  };

  useEffect(() => {
    setChartData({ ...limit, type, ...fondValue });
    setListQuery((state) => {
      return { ...state, pageNo: 1 };
    });
  }, [limit, type, fondValue]);

  useEffect(() => {
    const param = { ...limit, type, ...fondValue };
    setTableData({ ...listQuery, param });
  }, [limit, type, fondValue, listQuery]);

  useEffect(() => {
    setVisibleData(dataList);
    setYData(chartList.map((item: any) => item.name));
    setXData(chartList.map((item: any) => item[segmented]));
  }, [dataList, chartList, segmented]);

  const onChangeNav = (value: any) => {
    setFondValue(value);
    console.log('value', value);
  };

  const ChangNo = (num: any) => {
    console.log('num', num);
    setListQuery((state) => {
      return { ...state, pageNo: num };
    });
  };
  useEffect(() => {
    console.log('table', table);
    const removed = table.slice(0);
    if (type === 0) {
      removed.splice(
        1,
        0,
        { title: '责任部门', dataIndex: 'responsibleDept', key: 'responsibleDept' },
        { title: '原格式', dataIndex: 'sourceFormat', key: 'sourceFormat' },
        { title: '目标格式', dataIndex: 'targetFormat', key: 'targetFormat' }
      );
      removed.push({ title: '转化文件数量', dataIndex: 'fileConverTotal', key: 'fileConverTotal' });
      setTableList(removed);
      setOptions([
        {
          title: '责任部门',
          name: 'responsibleDept',
          checkList: [
            { value: 1, label: '部门1' },
            { value: 2, label: '部门2' },
            { value: 3, label: '部门3' },
            { value: 4, label: '部门4' },
            { value: 5, label: '部门5' }
          ]
        },
        {
          title: '原格式',
          name: 'sourceFormat',
          checkList: [
            { value: 1, label: '格式1' },
            { value: 2, label: '格式2' },
            { value: 3, label: '格式3' },
            { value: 4, label: '格式4' },
            { value: 5, label: '格式5' }
          ]
        },
        {
          title: '目标格式',
          name: 'targetFormat',
          checkList: [
            { value: 1, label: 'PDF' },
            { value: 2, label: 'OFD' }
          ]
        }
      ]);
      setCondition(<></>);
    }
    if (type === 1) {
      removed.splice(1, 0, { title: '授权单位', dataIndex: 'authUnit', key: 'authUnit' });
      removed.push(
        { title: 'OFD转换数量', dataIndex: 'totalOfOFD', key: 'totalOfOFD' },
        { title: 'PDF转换数量', dataIndex: 'totalOfPDF', key: 'totalOfPDF' }
      );
      setTableList(removed);
      setOptions([
        {
          title: '授权单位',
          name: 'authUnit',
          checkList: [
            { value: 1, label: '授权单位1' },
            { value: 2, label: '授权单位2' },
            { value: 3, label: '授权单位3' },
            { value: 4, label: '授权单位4' },
            { value: 5, label: '授权单位5' }
          ]
        }
      ]);
      setCondition(<></>);
    }
    if (type === 2) {
      removed.splice(1, 0, { title: '操作者', dataIndex: 'operatorName', key: 'operatorName' });
      removed.push(
        { title: 'OFD转换数量', dataIndex: 'totalOfOFD', key: 'totalOfOFD' },
        { title: 'PDF转换数量', dataIndex: 'totalOfPDF', key: 'totalOfPDF' }
      );
      setTableList(removed);
      setOptions([
        {
          title: '人员筛选',
          name: 'operator',
          checkList: [
            { value: 1, label: '人员1' },
            { value: 2, label: '人员2' },
            { value: 3, label: '人员3' },
            { value: 4, label: '人员4' },
            { value: 5, label: '人员5' }
          ]
        }
      ]);
      setCondition(<></>);
    }
  }, [type]);

  /* 改变触发 */
  const onChangeYear = (value: any) => {
    setLimit(value.chioce);
    setSegmented(value.segmentValue);
  };

  return (
    <>
      <Tabs tabPosition="left" className={cx('tab')} onTabClick={handleAccordingChange}>
        {according.map((item) => (
          <TabPane tab={<>{item.fondName}</>} key={item.id}>
            <VisualizationMain
              type={item.fondName}
              condition={condition}
              segmented={segmentedList}
              tableList={tableList}
              choiceList={options}
              data={{ visibleData, xData, yData }}
              onChange={onChangeYear}
              ChangNo={ChangNo}
            />
          </TabPane>
        ))}
      </Tabs>
    </>
  );
};
export default FormatConversionWarp;
